package cn.highedu.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("aware")
@Scope
public class AwareBean implements ApplicationContextAware, BeanNameAware {
    private ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void getBean() {
        // 获取Spring容器中管理的对象
        TagService tagService = applicationContext.getBean("tagService", TagService.class);
        System.out.println(tagService.getTags().toString());
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("获取到对象标识:" + s);
    }
}
